package cs342.group1.messenger;

import java.util.List;

import com.example.grouponemessenger.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConversationAdapter extends ArrayAdapter<Message>
{
	private TextView tv;
	private List<Message> messages;
	private LinearLayout wrap;
	
	public ConversationAdapter(Context context, int textViewResourceId, List<Message> messages)
	{
		super(context, textViewResourceId, messages);
		this.messages = messages; 
	}
	
	public int getCount()
	{
		return this.messages.size();
	}
	
	public Message getItem(int index)
	{
		return this.messages.get(index);
	}
	
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View adpRow = convertView;
		if (adpRow == null)
		{
			LayoutInflater lf = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			adpRow = lf.inflate(R.layout.my_messages_list_item, parent, false);
		}
		this.wrap = (LinearLayout)adpRow.findViewById(R.id.messageLayout);
		LinearLayout w = (LinearLayout)wrap.findViewById(R.id.wrapper);
		Message message = getItem(position);
		tv = (TextView)adpRow.findViewById(R.id.myMessagesItemContent);
		tv.setText(message.toString());
		
		w.setBackgroundResource(message.FromUserId == Conversation.staticID ? R.drawable.bubble_me : R.drawable.bubble_them);
		wrap.setGravity(message.FromUserId == Conversation.staticID ? Gravity.RIGHT : Gravity.LEFT);
		
		return adpRow;
	}
	
	public Bitmap decodeToBitmap(byte[] decodeByte)
	{
		return BitmapFactory.decodeByteArray(decodeByte, 0, decodeByte.length);
	}
}